<template>
  <div class="filters" v-if="options">
    <el-row type="flex" class="filters-top" justify="space-between" align="middle">
      <el-col :span="8">
        单程：
        {{$route.query.departCity}} - {{$route.query.destCity}}
        /
        {{$route.query.departDate}}
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airport" placeholder="起飞机场" @change="totalFilter">
          <el-option
            v-for="(item,index) in options.airport"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="flightTimes" placeholder="起飞时间" @change="totalFilter">
          <el-option
            v-for="(item,index) in options.flightTimes"
            :key="index"
            :label="`${item.from}:00 - ${item.to}:00`"
            :value="`${item.from}, ${item.to}`"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="company" placeholder="航空公司" @change="totalFilter">
          <el-option
            v-for="(item,index) in options.company"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airSize" placeholder="机型" @change="totalFilter">
          <el-option
            v-for="(item,index) in planeSize"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="filter-cancel">
      筛选：
      <el-button type="primary" round plain size="mini" @click="handleFiltersCancel">撤销</el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: ["options", "flights"],
  data() {
    return {
      airport: "", // 机场
      flightTimes: "", // 出发时间
      company: "", // 航空公司
      airSize: "", // 机型大小
      planeSize: [
        {
          label: "大",
          value: "L"
        },
        {
          label: "中",
          value: "M"
        },
        {
          label: "小",
          value: "S"
        }
      ]
    };
  },
  methods: {
    totalFilter() {
      //每次复制一份100份的原来数组,就是为了共同协作.比如防止点了东航再点国航就没有,下面利用判断来检测是否还有其他的过滤,有的话共同过滤.
      var newList = [...this.flights];

      if (this.airport) {
        newList = newList.filter(item => {
          return item.org_airport_name == this.airport;
        });
      }

      //时间
      if (this.flightTimes) {
        newList = newList.filter(item => {
          const planeTime = Number(item.dep_time.split(":")[0]);
          const from = Number(this.flightTimes.split(",")[0]);
          const to = Number(this.flightTimes.split(",")[1]);
          return planeTime >= from && planeTime < to;
        });
      }
      //航空公司
      if (this.company) {
        newList = newList.filter(item => {
          return item.airline_name == this.company;
        });
      }
      //机型大小
      if (this.airSize) {
        newList = newList.filter(item => {
          return item.plane_size == this.airSize;
        });
      }
      //最后全部过滤发送
      this.$emit("setFilter", newList);
    },

    // 撤销条件时候触发
    handleFiltersCancel() {
      this.airport = "";
      this.flightTimes = "";
      this.company = "";
      this.airSize = "";
      this.totalFilter();
    }
  }
};
</script>

<style scoped lang="less">
.filters {
  margin-bottom: 20px;
}

.filters-top {
  > div {
    /deep/ .el-select {
      margin-left: 10px;
    }
  }
}

.filter-cancel {
  margin-top: 10px;
}
</style>